package com.zsy.controller;

import com.zsy.entity.User;
import com.zsy.exception.CustomException;
import com.zsy.mapper.IUserMapper;
import com.zsy.utils.Result;
import com.zsy.service.impl.JwtAuthService;
import com.zsy.utils.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletWebRequest;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
public class JwtAuthController {

    @Resource
    private JwtAuthService jwtAuthService;

    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @Resource
    private IUserMapper ium;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @PostMapping(value = "/authentication")
    public Result login(@RequestBody Map<String,String> request) throws ServletRequestBindingException {
//        String username = ServletRequestUtils.getStringParameter(request.getRequest(),"username");
//        String password = ServletRequestUtils.getStringParameter(request.getRequest(),"password");
//        String codeInSession = (String) session.getAttribute("code");

        //手机号发送验证码
//        String code = (String) redisTemplate.opsForValue().get("code");
//        if (!code.equals(request.get("code")) || code == null){
//            return Result.error("验证码错误");
//        }
        System.out.println(request);
        String code1 = CaptchaController.code;
        System.out.println("测试code："+code1);
        String username = request.get("username");
        String password = request.get("password");
//        String code = request.get("captchaCode");
        String remember = request.get("remember-me");
        System.out.println("username:"+username);
        System.out.println("password:"+password);
//        System.out.println("code:"+code);
        System.out.println("remember:"+remember);

        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            return Result.error("用户名密码不能为空");
        }
        try{
            User user = ium.selectByUsername(username);
            UserDetails userDetails = jwtAuthService.login(username, password);
            System.out.println("令牌账号密码"+userDetails.getUsername()+userDetails.getPassword());

            String token = jwtTokenUtil.generateToken(userDetails);
            HashMap<String, Object> map1 = new HashMap<>();
            map1.put("token", token);
            map1.put("username", userDetails.getUsername());
            map1.put("phone",user.getPhone());
            map1.put("department",user.getDepartment());

            return Result.ok(map1);
        }catch(CustomException e){
            e.printStackTrace();
            return Result.error("认证失败");
        }
    }

    @PostMapping(value = "/refreshtoken")
    public Result refresh(@RequestHeader("JWTHeaderName") String token) {
        return Result.ok(jwtAuthService.refreshToken(token));
    }

}